<?php

class Student_Model_DbTable_GPA extends Zend_Db_Table_Abstract {

	protected $_name = 'student_course';

        //protected $_dbAlias = "";

        public function GPA($user_id, $thisSemester, $thisYear)
        {
           $GPA = new Student_Model_DbTable_Tracker();
           $user_courses = new Student_Model_DbTable_Course();
           $scale = new Student_Model_DbTable_Scale();
           $theGPA = new Student_Model_DbTable_guestGPA();
           //$theGPA = Student_Model_DbTable_guestGPA();

                      //gets the courses for that user
           $get_courses = $user_courses->getSingleUserCourses($user_id);
           
           //get the grade scale
           $avg=0;
           $gradeArray = array();
           $creditsArray = array();
           $grade = 0;
           $scale_id = 0;

           foreach($get_courses as $gc){

               //print_r($gc->year);
               if ($gc->year == $thisYear){
                   //print_r($thisYear);
                    if ($gc->semester == $thisSemester){

                    //print_r($thisSemester);

                    //current average of that course
                    $avg = $GPA->currentAverage($gc->id);
                   //print_r("$avg");

                    #something's messed up from here on
                    $get_scale = $scale->getCourseScale($gc->id);

                   // print_r($get_scale);
                    
                    if(!$avg == 0){
                        //$row = null;
                        foreach($get_scale as $gs){
                             //$row = $this->fetchRow('id');
                            // while(!row){
                             //    $found = $row->toArray();

                                // print_r($found);
                            
                           // print_r($gs->id);
                            //print_r($gs->minimum);
                           // print_r($gs->maximum);
                           // print_r($gs->grade);
                           // print_r("\n");
                                                                             
                            if($avg >= $gs->minimum){
                                
                                 if ($avg <= $gs->maximum){

                                                                    
                                    $grade = $gs->grade;

                                    }
                                    
                                }
                                }
                            }
                    }

               }

               
               if(!$grade ==null){
               array_push($gradeArray, $grade);
               array_push($creditsArray, $gc->course_credits);
               }
               $grade = 0;
           }
                  
          $currentGPA = $theGPA->currentGPA($gradeArray, $creditsArray);
          print_r($currentGPA);
          return $currentGPA;
}
}
